import 'dart:io';

import 'package:dio/dio.dart';

typedef Scuess = void Function(dynamic json);
typedef Fail  = void Function(dynamic statuscode,dynamic statusMessage);

class Http{

  static Dio _dio;

  static Http http = Http();

  Map<String, dynamic> deviceData = <String, dynamic>{};


  static Http getInsntance(){
     return http;
  }

  HTTP() {
    if(_dio== null){
      _dio =  createDio();
    }
  }

  // Future<void> initPlatformState() async {
  //   Map<String, dynamic> deviceData = <String, dynamic>{};
  //   try {
  //     if (Platform.isAndroid) {
  //       deviceData = await deviceInfoPlugin.androidInfo;
  //     } else if (Platform.isIOS) {
  //       deviceData = await deviceInfoPlugin.iosInfo;
  //     }
  //   } on PlatformException {
  //     deviceData = <String, dynamic>{
  //       'Error:': 'Failed to get platform version.'
  //     };
  //   }

  Dio createDio() {
    var dio = Dio(BaseOptions(
      connectTimeout: 3000,
      responseType: ResponseType.json,
      receiveTimeout: 1000,
      headers: deviceData
    ));
    return dio;
  }

   void get(String url,{parama,required Scuess scuess,required Fail failure}) {
      _dio.get(url,queryParameters: parama).then((response){
        if(response.statusCode == 200){
           scuess(response.data);
        }else{
          failure(response.statusCode,response.statusMessage);
        }
      });
  }

}

main(){
  Http.getInsntance().get("", scuess: (json){
    print(json);
  }, failure: (code,message){
    print("${code},${message}");

  });
}